package tech.aistar.day10;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 本类用来演示:Collection和Collections区别
 *
 * @author: success
 * @date: 2021/3/22 1:34 下午
 */
public class CollectionsDemo {
    public static void main(String[] args) {
        List<Book> bookList = new ArrayList<>();

        Book b1 = new Book(1,"1001","西游记",100.0d);
        Book b2 = new Book(3,"1003","西游记3",300.0d);
        Book b3 = new Book(2,"1002","西游记2",100.0d);
        Book b4 = new Book(4,"1004","西游记4",200.0d);

        bookList.add(b1);
        bookList.add(b2);
        bookList.add(b3);
        bookList.add(b4);

        //反转
        //Collections.reverse(bookList);

        //使用集合工具类
        //1. 随机打乱
        //Collections.shuffle(bookList);

        //2. 排序
        //笔试 - 集合的排序的两种方式
        //1. 使用了一个java.util.Comparator接口

//        Collections.sort(bookList, new Comparator<Book>() {
//            @Override
//            public int compare(Book o1, Book o2) {
//                return 0;
//            }
//        });

        //根据价格降序排列
//        Collections.sort(bookList,(o1,o2)->{
//            if(o1.getPrice()>o2.getPrice()){
//                //return -1;//降序...
//                return 1;//升序
//            }else if(o1.getPrice()<o2.getPrice()){
//                return -1;
//            }else{
//                return 0;
//            }
//        });

        //jdk8.x开始以后
//        bookList.sort(new Comparator<Book>() {
//            @Override
//            public int compare(Book o1, Book o2) {
//                return 0;
//            }
//        });

        //bookList.sort((o1,o2)->(int)(o2.getPrice()-o1.getPrice()));



        //bookList.forEach(e-> System.out.println(e));
//        String type = ComparatorFactory.BOOK_ISBN;
//
//        Comparator<Book> c = ComparatorFactory.sort(type);
//
//        bookList.sort(c);

        //Collections.sort(bookList);

        for (Book book : bookList) {
            System.out.println(book);
        }
    }
}
